package com.d6game.web.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import weibo4j.http.AccessToken;

import com.d6game.web.annotation.LoginRequired;


public class LoginAnnotationInterceptor extends HandlerInterceptorAdapter {
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception{
		if( handler instanceof HandlerMethod){
			HandlerMethod handler2 = (HandlerMethod) handler;
			String className = handler2.getBean().getClass().getName();
			LoginRequired loginRequired = handler2.getMethodAnnotation(LoginRequired.class);
			if(null == loginRequired){
				return true;
			}
			if (className.contains("WeiboController")) {
				HttpSession session = request.getSession();
				AccessToken accessToken = (AccessToken) session.getAttribute("accessToken");
				if (accessToken == null) {
					response.sendRedirect("/");
					return false;
				}
			}
			return true;
		}else {
			return true;
		}
	}
}
